package com.ymatou.search;

import com.android.uiautomator.core.UiCollection;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.ymatou.util.Logger;

@SuppressWarnings("deprecation")
public class SearchResultFilterTest extends UiAutomatorTestCase{
	private static final String TAG = SearchResultFilterTest.class.getSimpleName();
	
	public void testSearchResultFilter() throws UiObjectNotFoundException, InterruptedException {
		
		UiSelector lv_filter_content_s = new UiSelector().resourceId("com.ymatou.shop:id/lv_filter_content");
		UiCollection lv_filter_content_c = new UiCollection(lv_filter_content_s);
		UiSelector tv_prod_filter_group_name_childPattern = new UiSelector().resourceId("com.ymatou.shop:id/tv_prod_filter_group_name");
		UiSelector fgv_prod_filter_group_content_childPattern = new UiSelector().resourceId("com.ymatou.shop:id/fgv_prod_filter_group_content");
		
		int tv_prod_filter_group_name_count = lv_filter_content_c.getChildCount(tv_prod_filter_group_name_childPattern);
		
		for (int i = 0; i < tv_prod_filter_group_name_count; i++) {
			UiObject tv_prod_filter_group_name_o = lv_filter_content_c.getChildByInstance(tv_prod_filter_group_name_childPattern, i);
			
			if (tv_prod_filter_group_name_o.getText().equalsIgnoreCase("价格区间")) {
				continue;
			}
			
			this.testClickFilter(lv_filter_content_c, fgv_prod_filter_group_content_childPattern, i);
			
			Logger.log(TAG, tv_prod_filter_group_name_o.getText());
		}
		
		//UiScrollable lv_filter_content_scroll = new UiScrollable(lv_filter_content_s);
		//滚动列表
		//lv_filter_content_scroll.scrollToEnd(500);
		
		//测试完成后点击确认
		UiSelector tv_live_prod_filter_action_confirm_s = new UiSelector().
				resourceId("com.ymatou.shop:id/tv_live_prod_filter_action_confirm");
		UiObject tv_live_prod_filter_action_confirm_o = new UiObject(tv_live_prod_filter_action_confirm_s);
		
		tv_live_prod_filter_action_confirm_o.click();
		//Logger.log(TAG, "点击" + tv_live_prod_filter_action_confirm_o.getText());
	}
	
	public void testClickFilter(UiCollection collection, UiSelector selector, int i) throws UiObjectNotFoundException {
		UiObject fgv_prod_filter_group_content_o = collection.getChildByInstance(selector, i);
		
		UiCollection fgv_prod_filter_group_content_c = new UiCollection(fgv_prod_filter_group_content_o.getSelector());
		UiSelector childPattern = new UiSelector().className("android.widget.TextView");
		
		int fgv_prod_filter_group_content_count = fgv_prod_filter_group_content_c.getChildCount(childPattern);
		
		for (int j = 0; j < fgv_prod_filter_group_content_count; j++) {
			UiObject object = fgv_prod_filter_group_content_c.getChildByInstance(childPattern, j);
			boolean isClick = false;
			if (j == 0) {
				object.click();
				Logger.log(TAG, "点击" + object.getText());
				isClick = true;
			}
			//Thread.sleep(3000);
			Logger.log(TAG, object.getText());
			
			if (isClick)
				object.click();
		}
	}
}
